package com.zhida.jiyangcenter.order.adapter

import android.view.View
import android.widget.ImageView
import androidx.appcompat.widget.AppCompatImageView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.zhida.jiyangcenter.R
import com.zhida.jiyangcenter.utils.glideImage
import java.util.*

class CommentAdapter(mPhotoList: ArrayList<PhotoInfo>, val mDelPhotoListener: DelPhotoListener) : BaseQuickAdapter<PhotoInfo, BaseViewHolder>(
    R.layout.item_publish_photo, mPhotoList) {
    private val isEdit = true

    override fun convert(helper: BaseViewHolder, item: PhotoInfo) {
        val photo = helper.getView<ImageView>(R.id.img_photo)
        val del = helper.getView<AppCompatImageView>(R.id.img_del)
        del.visibility = View.VISIBLE
        val filePath = item.localUrl
        del.visibility = View.INVISIBLE
        if ("发布" == filePath) {
            photo.setImageResource(R.drawable.ic_select_camera)
            photo.setOnClickListener { v -> mDelPhotoListener.addPhoto() }
            photo.setOnLongClickListener { v -> false }
        } else {
            glideImage(photo, filePath)
            del.setOnClickListener { v -> mDelPhotoListener.delPhoto(filePath) }
            photo.setOnLongClickListener { v ->
                if ("发布" != filePath && isEdit) {
                    del.visibility = View.VISIBLE
                }
                true
            }
            photo.setOnClickListener { view ->
                /*因为holder有缓存，如果此时不设置空的点击事件，
            上一个发布按钮的点击事件还在，将会导致，点击图片显示拍照选择*/
            }
        }
    }

    interface DelPhotoListener {
        fun delPhoto(path: String)
        fun addPhoto()
    }
}